﻿using System;
using Microsoft.Crm.Sdk;

namespace Microsoft.CRM.ThirdParty.Extensions
{
    public static class DynamicEntityExtensions
    {

        public static int GetIntegerStatusValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Status)(entity.Properties[property])).Value;
            return Int32.MinValue;
        }

        public static Guid GetLookupAsGuidValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return new Guid(((Lookup)(entity.Properties[property])).Value.ToString());
            return Guid.Empty;
        }

        public static string GetStringStatusValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Status)(entity.Properties[property])).name;
            return String.Empty;
        }

        public static string GetStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return entity.Properties[property].ToString();
            return String.Empty;
        }

        public static string GetKeyAsStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Key)(entity.Properties[property])).Value.ToString();
            return String.Empty;
        }

        public static Guid GetKeyAsGuidValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return new Guid(((Key)(entity.Properties[property])).Value.ToString());
            return Guid.Empty;
        }

        public static int GetIntegerValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmNumber)(entity.Properties[property])).Value;
            return Int32.MinValue;
        }

        public static int GetPicklistValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).Value;
            return Int32.MinValue;
        }

        public static string GetPicklistNameOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).name;
            return String.Empty;
        }

        public static double GetFloatValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmFloat)(entity.Properties[property])).Value;
            return Double.MinValue;
        }

        public static double GetDoubleValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDouble)(entity.Properties[property])).Value;
            return Double.MinValue;
        }

        public static decimal GetDecimalValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDecimal)(entity.Properties[property])).Value;
            return Decimal.MinValue;
        }

        public static decimal GetMoneyValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmMoney)(entity.Properties[property])).Value;
            return Decimal.MinValue;
        }

        public static DateTime GetDateTimeValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDateTime)(entity.Properties[property])).UserTime;
            return DateTime.MinValue;
        }

        public static void ApplyActionTo(this DynamicEntity entity, Action<DynamicEntity> action)
        {
            action(entity);
        }

        public static bool PropertyExistedButNotChanged(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property)
        {
            var oldcontains = old_employee_entity.Properties.Contains(property);
            var currentcontains = current_employee_entity.Properties.Contains(property);

            bool changed = true;

            if (oldcontains && !currentcontains)
                changed = false;

            return changed;
        }

        public static bool BothContainProperty(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property)
        {
            var oldcontains = old_employee_entity.Properties.Contains(property);
            var currentcontains = current_employee_entity.Properties.Contains(property);

            return oldcontains && currentcontains;
        }

        public static bool PropertyAdded(DynamicEntity current_employee_entity, DynamicEntity old_employee_entity, string property)
        {
            var oldcontains = old_employee_entity.Properties.Contains(property);
            var currentcontains = current_employee_entity.Properties.Contains(property);

            bool changed = false;

            if (!oldcontains && currentcontains)
                changed = true;

            return changed;
        }

        public static void AssignStringValueForField(this DynamicEntity entity, string property, string value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = value;
            else
                entity.Properties.Add(new StringProperty(property, value));
        }

        public static void AssignBooleanValueForField(this DynamicEntity entity, string property, bool value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new CrmBoolean(value);
            else
                entity.Properties.Add(new CrmBooleanProperty(property, new CrmBoolean(value)));
        }

        public static void AssignPicklistValueForField(this DynamicEntity entity, string property, int value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new Picklist(value);
            else
                entity.Properties.Add(new PicklistProperty(property, new Picklist(value)));
        }

        public static void AssignFloatValueForField(this DynamicEntity entity, string property, double value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new CrmFloat(value);
            else
                entity.Properties.Add(new CrmFloatProperty(property, new CrmFloat(value)));
        }

        public static void AssignDecimalValueForField(this DynamicEntity entity, string property, decimal value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new CrmDecimal(value);
            else
                entity.Properties.Add(new CrmDecimalProperty(property, new CrmDecimal(value)));
        }

        public static void AssignMoneyValueForField(this DynamicEntity entity, string property, decimal value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new CrmMoney(value);
            else
                entity.Properties.Add(new CrmMoneyProperty(property, new CrmMoney(value)));
        }

        public static void AssignNumberValueForField(this DynamicEntity entity, string property, int value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = new CrmNumber(value);
            else
                entity.Properties.Add(new CrmNumberProperty(property, new CrmNumber(value)));
        }

        public static void AssignDateTimeValueForField(this DynamicEntity entity, string property, DateTime value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = CrmDateTime.FromUniversal(value);
            else
                entity.Properties.Add(new CrmDateTimeProperty(property, CrmDateTime.FromUniversal(value)));
        }

        public static void AssignLookupValueForField(this DynamicEntity entity, string property, Lookup value)
        {
            if (entity.Properties.Contains(property))
                entity.Properties[property] = value;
            else
                entity.Properties.Add(new LookupProperty(property, value));
        }

    }
}
